﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "Widget/SFilletButton.h"
#include "SlateOptMacros.h"

BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION

void SFilletButton::Construct(const FArguments& InArgs)
{
	Text = InArgs._Text;
	TextFont = InArgs._TextFont;

	struct
	{
		TSharedRef<SWidget> operator()( const FArguments& InOpArgs, TSharedPtr<STextBlock>& InButtonText) const
		{
			if ((InOpArgs._Content.Widget == SNullWidget::NullWidget) && (InOpArgs._Text.IsBound() || !InOpArgs._Text.Get().IsEmpty()) )
			{
				InButtonText = SNew(STextBlock)
                    .Visibility(EVisibility::HitTestInvisible)
                    .Text( InOpArgs._Text )
                    .Font( InOpArgs._TextFont );
				return InButtonText.ToSharedRef();
			}
			else
			{
				return InOpArgs._Content.Widget;
			}
		}
	} DetermineContent;
	
	ChildSlot
	[
		SAssignNew(Button,SButton)
		.ButtonColorAndOpacity(FLinearColor(1.f,1.f,1.f,0.f))
		.ContentPadding(0.f)
		.HAlign(EHorizontalAlignment::HAlign_Fill)
		.VAlign(EVerticalAlignment::VAlign_Fill)
		.OnClicked(InArgs._OnClicked)
		.OnHovered(InArgs._OnHovered)
		.OnUnhovered(InArgs._OnUnhovered)
		[
			SAssignNew(ButtonBorder, SBorder)
    		.HAlign( InArgs._HAlign )
    		.VAlign( InArgs._VAlign )
    		[
					
    		    DetermineContent(InArgs,ButtonText)
    		]
		]
		
	];
	FButtonStyle buttonStyle = FButtonStyle()
        .SetNormalPadding(FMargin(0))
        .SetPressedPadding(FMargin(0));
	Button->SetButtonStyle(&buttonStyle);
	
	// SBorder::Construct( SBorder::FArguments()
	//     .ContentScale(InArgs._ContentScale)
	//     .DesiredSizeScale(InArgs._DesiredSizeScale)
	//     .BorderBackgroundColor(InArgs._ButtonColorAndOpacity)
	//     .ForegroundColor(InArgs._ForegroundColor)
	//     .BorderImage( this, &SButton::GetBorder )
	//     .HAlign( InArgs._HAlign )
	//     .VAlign( InArgs._VAlign )
	//     .Padding( TAttribute<FMargin>(this, &SButton::GetCombinedPadding) )
	//     .ShowEffectWhenDisabled( TAttribute<bool>(this, &SButton::GetShowDisabledEffect) )
	//     [
	//         DetermineContent(InArgs)
	//     ]
	// );
}

void SFilletButton::SetBorderImage(const FSlateBrush* IconBrush)
{
	if (ButtonBorder.IsValid())
	{
		ButtonBorder->SetBorderImage(IconBrush);
	}
}

void SFilletButton::ResetButton()
{
	ButtonBorder->SetContent(ButtonText.ToSharedRef());
}

void SFilletButton::SetText(const FText& value)
{
	Text = value;
	ButtonText->SetText(Text);
}

void SFilletButton::SetTextFont(const FSlateFontInfo& value)
{
	TextFont = value;
	ButtonText->SetFont(TextFont);
}

void SFilletButton::SetTextColorAndOpacity(const FLinearColor& value)
{
	ButtonText->SetColorAndOpacity(value);
}

void SFilletButton::HideDefaultSlate()
{
	SetTextColorAndOpacity(FLinearColor(1.f,1.f,1.f,0.f));
}

void SFilletButton::ShowDefaultSlate()
{
	SetTextColorAndOpacity(FLinearColor(1.f,1.f,1.f,1.f));
}

void SFilletButton::SetContentPadding(const FMargin& value)
{
	if (ButtonBorder.IsValid())
	{
		ButtonBorder->SetPadding(value);
	}
}

void SFilletButton::SetHAlign(EHorizontalAlignment value)
{
	if (ButtonBorder.IsValid())
	{
		ButtonBorder->SetHAlign(value);
	}
}

void SFilletButton::SetVAlign(EVerticalAlignment value)
{
	if (ButtonBorder.IsValid())
	{
		ButtonBorder->SetVAlign(value);
	}
}

void SFilletButton::SetContent(TSharedRef<SWidget> InContent)
{
	if (ButtonBorder.IsValid())
	{
		ButtonBorder->SetContent(InContent);
	}
}

END_SLATE_FUNCTION_BUILD_OPTIMIZATION
